﻿using EasyExcel;

using Model.Data;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Utilities;

namespace WssClient.Item.XinDaXiang
{
    public class XinDaXiang : Item<XinDaXiang, SalesStockOut>
    {
        public override void Handler(RequestData requestData)
        {
            SalesStockOut salesStockOut = new SalesStockOut();

            salesStockOut.Analysis(requestData);
            Print(salesStockOut);
        }

        public override XinDaXiang Init()
        {
            this.title = "鑫大橡";
            this.type = ItemType.Type.XinDaXiang;
            this.excelName = "xindaxiang.xlsx";

            return this;
        }

        public override void Print(SalesStockOut salesStockOut)
        {
            ExcelDoc Excel = new ExcelDoc(GVar.item.excelName);

            Excel.SetCell(1, 1, salesStockOut.OrderId);
            Excel.SetCell(1, 7, salesStockOut.CatNo);
            Excel.SetCell(2, 1, salesStockOut.TakeSite);
            Excel.SetCell(2, 7, salesStockOut.OrderDate);
            Excel.SetCell(3, 1, salesStockOut.Customer);
            Excel.SetCell(3, 7, salesStockOut.Tel);

            int idx = 5;

            List<Product> generalProducts = salesStockOut.GeneralProducts;
            int count = generalProducts.Count;
            int nextIdx = count > 0 ? idx : idx;

            if(count > 0)
            {
                for(int i = 0; i < count; i++)
                {
                    Product product = generalProducts[i];
                    Excel.SetCell(nextIdx, 0, product.Idx); //序号
                    Excel.SetCell(nextIdx, 1, product.Info);    //商品信息
                    Excel.SetCell(nextIdx, 4, product.Packages);    //件数
                    Excel.SetCell(nextIdx, 5, product.UnitsNumber);  //单位数量
                    Excel.SetCell(nextIdx, 6, product.Number);   //数量
                    Excel.SetCell(nextIdx, 7, product.Units);   //单位
                    Excel.SetCell(nextIdx, 8, product.Price);   //单价
                    Excel.SetCell(nextIdx, 9, product.Total);   //合计
                    Excel.SetCell(nextIdx, 10, product.Remark); //备注

                    nextIdx++;

                    if(i < count - 1)
                    {
                        Excel.CopyRow(idx, nextIdx);
                    }
                }
            }

            List<Product> monkeyHeavyProducts = salesStockOut.MonkeyHeavyProducts;
            int count1 = monkeyHeavyProducts.Count;

            if(count1 > 0)
            {
                if(count > 0)
                {
                    Excel.CopyRow(idx, nextIdx);
                }
                for(int i = 0; i < count1; i++)
                {
                    Product product = monkeyHeavyProducts[i];
                    Excel.SetCell(nextIdx, 0, product.Idx); //序号
                    Excel.SetCell(nextIdx, 1, product.Info);    //商品信息
                    Excel.SetCell(nextIdx, 4, product.Packages);    //件数
                    Excel.SetCell(nextIdx, 5, product.UnitsNumber);  //单位数量
                    Excel.SetCell(nextIdx, 6, product.Number);   //数量
                    Excel.SetCell(nextIdx, 7, product.Units);   //单位
                    Excel.SetCell(nextIdx, 8, product.Price);   //单价
                    Excel.SetCell(nextIdx, 9, product.Total);   //合计
                    Excel.SetCell(nextIdx, 10, product.Remark); //备注

                    nextIdx++;

                    int detailsCount = product.Details.Count;
                    int totalCol = 11;
                    for(int j = 0; j < detailsCount; j++)
                    {
                        if(j == 0)
                        {
                            if(detailsCount > 1)
                            {
                                Excel.InsertBlankRow(nextIdx, totalCol, Excel.Npoi.InitCellStyle(isBottom: false));
                            }
                            else
                            {
                                Excel.InsertBlankRow(nextIdx, totalCol, Excel.Npoi.InitCellStyle());
                            }
                        }
                        else if(j < detailsCount - 1)
                        {
                            Excel.InsertBlankRow(nextIdx, totalCol, Excel.Npoi.InitCellStyle(false, isBottom: false));
                        }
                        else
                        {
                            Excel.InsertBlankRow(nextIdx, totalCol, Excel.Npoi.InitCellStyle(false));
                        }

                        Excel.Npoi.SetAlignLeft(nextIdx, 0);
                        Excel.SetCell(nextIdx, 0, product.Details[j]);
                        nextIdx++;
                    }

                    //Excel.InsertBlankRow(nextIdx, 12, Excel.Npoi.InitCellStyle());
                    //Excel.Npoi.SetAlignLeft(nextIdx, 0);
                    //Excel.SetCell(nextIdx++, 0, product.Detail);

                    if(i < count1 - 1)
                    {
                        Excel.CopyRow(idx, nextIdx);
                    }
                }

                //for (int i = 0; i < total; i++)
                //{
                //    Product product = monkeyHeavyProducts[i];
                //    Excel.InsertBlankRow(nextIdx, 11, Excel.Npoi.InitCellStyle());
                //    Excel.Npoi.SetAlignLeft(nextIdx, 0);
                //    Excel.SetCell(nextIdx++, 0, product.Detail);
                //}
            }

            Excel.SetCell(nextIdx, 2, salesStockOut.CurrentPaymentGoodsTotal);  //本期货款合计
            Excel.SetCell(nextIdx++, 6, salesStockOut.UpperCaseAmount);     //大写金额
            Excel.SetCell(nextIdx, 2, salesStockOut.QiChuQianKuanHeJi);   //期初账款合计
            Excel.SetCell(nextIdx++, 6, salesStockOut.PriorPeriodPaymentGoodsTotal);   //应收账款合计

            Excel.SetCell(nextIdx, 1, salesStockOut.Single); //制单人
            Excel.SetCell(nextIdx, 4, salesStockOut.DeliveryMan); //送货人

            LogHelper.Debug("鑫大橡打印");

            if(GVar.printMode)
            {
                Excel.Print();
            }
            else
            {
                Excel.PrintPreView();
            }
        }

        public override void Test()
        {
            throw new NotImplementedException();
        }
    }
}
